package com.vinjogames.ui;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.entities.Game;
import com.vinjogames.gfx.Camera;
import com.vinjogames.gfx.Font;
import com.vinjogames.gfx.Image;
import com.vinjogames.gfx.Shapes;
import com.vinjogames.util.GameConVars;
import com.vinjogames.util.Logger;

public class TechMenu {
	
	float mGLWidth;
	float mGLHeight;
	
	PointF mBotLeft;
	PointF mTopRight;
	PointF backgroundLocation;
	PointF budgetLocation;
	PointF resourcesRemainingLocation;
	PointF pageLocation;
	PointF mSelectedButtonLoc;

	PointF mMenuBorderCenter;
	PointF mSelectedBotLeft;
	PointF mSelectedTopRight;
	PointF mDiscBoxBotLeft;
	PointF mDiscBoxTopRight;
	
	Image mBackground;
	Image mHaveThisTech;
	Image mDoNotHaveThisTech;
	Image mMenuBorder;
	Image[][] mTechIcons;
	
	Game mGame;
	
	PressableButton[][][] gridButtons;
	PressableButton[] treeButtons;
//	PressableButton shopButton;
	PressableButton selectedButton;
	PressableButton backButton;
	PressableButton buyButton;
	
	
	boolean[] techSlots = {true, false, true, true, true, true, true, false};
	int[] realTechNum = {0, -1, 1, 2, 3, 4, 5, -1};
	
	int index = 1;
	int tree;
	int selectedButtonNum;
	
	int nextTree;
	int nextButtonNum;
	
	public TechMenu(Game game){
		mGame = game;
		mGLWidth = Camera.GL_WIDTH;
		mGLHeight = Camera.GL_HEIGHT;

		mBotLeft = new PointF(0,0);
		mTopRight = new PointF(mGLWidth,mGLHeight);
	}
	
	//TODO: Change up the inner for loop to switch between the types of stuff displayed in the buy menu
	//(namely units for the first 9 entries and then base upgrades for the last.)
	public void load(){
		tree = -1;
		selectedButtonNum = -1;
		nextTree = -1;
		nextButtonNum = -1;
		
		mSelectedButtonLoc = new PointF(2f, 4f);
		mMenuBorderCenter = new PointF(6, 3);
		mSelectedBotLeft = new PointF();
		mSelectedTopRight = new PointF();
		mDiscBoxBotLeft = new PointF(3, 1);
		mDiscBoxTopRight = new PointF(9, 5);

		backButton = new PressableButton(new Image(GameConVars.BUTTON, 3f, 1f), -2, new PointF(3f, 0f), new PointF(6f, 1f), false);
		backButton.setText("Back");
		buyButton = new PressableButton(new Image(GameConVars.BUTTON, 3f, 1f), -3, new PointF(6f, 0f), new PointF(9f, 1f), false);
		buyButton.setText("Buy");
		
		treeButtons = new PressableButton[GameConVars.TECH_TREES];
		backgroundLocation = new PointF(mBotLeft.x + (mTopRight.x - mBotLeft.x)/2, 
                mBotLeft.y + (mTopRight.y - mBotLeft.y)/2);
		budgetLocation = new PointF(8,4.75f);
		resourcesRemainingLocation = new PointF(8,4.25f);
		
		mBackground = new Image(GameConVars.MENUBACKGROUND,mGLWidth, mGLHeight);
		
		gridButtons = new PressableButton[GameConVars.TECH_TREES][GameConVars.TECH_DEPTH][2];
		
		for(int treeIndex = 0; treeIndex < GameConVars.TECH_TREES; ++treeIndex){
			treeButtons[treeIndex] = new PressableButton(mDoNotHaveThisTech,
					treeIndex,
					new PointF(1 + 3 * treeIndex, 3),
					new PointF(3 + 3 * treeIndex, 5), true);
			treeButtons[treeIndex].setText("Tree: " + treeIndex);
		}
		
		for(int page = 0; page < GameConVars.TECH_TREES; ++page)
			for(int row = 0; row < GameConVars.TECH_DEPTH; ++row)
				for(int column = 0; column < 2; ++column) {
					if(column + row * 2 >= GameConVars.GRAPHIC_TANKS.length) return;
					gridButtons[page][row][column] = new PressableButton(mDoNotHaveThisTech,
									(page * realTechNum.length + column + row * 2),
									// (0.25, 2.25)(2.75, 4.75)(5.25, 7.25)(7.75, 9.75)(
									new PointF(1f + row * 2f, 3 - column * 2f),
									new PointF(3f + row * 2f, 5 - column * 2f),
									true);
//									new PointF(row * GameConVars.BUY_BUTTON_WIDTH + page * GameConVars.BUY_BUTTON_WIDTH * 3, column * GameConVars.BUY_BUTTON_HEIGHT),
//									new PointF((row + 1) * GameConVars.BUY_BUTTON_WIDTH + page * GameConVars.BUY_BUTTON_WIDTH * 3, (column + 1) * GameConVars.BUY_BUTTON_HEIGHT), true);
					
					gridButtons[page][row][column].setText("Res:" + GameConVars.BUY_TECHNOLOGY_COSTS[row]);
				}

		// To be used when individual graphics are ready.
		mTechIcons = new Image[GameConVars.TECH_TREES][GameConVars.TECH_TREE_TOTALS];
		for(int trees = 0; trees < GameConVars.TECH_TREES; ++trees){
			for(int tech = 0; tech < GameConVars.TECH_TREE_TOTALS; ++tech){
				// For individual icons
//				mTechIcons[trees][tech] = new Image(GameConVars.GRAPHIC_TECHTREE_OFFSET + trees*GameConVars.TECH_TREE_TOTALS + tech, 2f, 2f);
				mTechIcons[trees][tech] = new Image(GameConVars.GRAPHIC_BUTTON_BORDER, 2f, 2f);
			}
		}
		mMenuBorder = new Image(GameConVars.GRAPHIC_WINDOW_BORDER, 6f, 4f);
//		shopButton = new PressableButton(new Image(GameConVars.GRAPHIC_BACKGROUND1, mTopRight.x, 1), -1, new PointF(0,mTopRight.y - 1), mTopRight, false);
//		shopButton.setText("Shop Menu");
	}
	
	public void draw(GL10 gl){
		mBackground.draw(gl, backgroundLocation);
		Font.drawString(gl, "Budget: " + (mGame.getCurrentPlayer().getMyTechnologyTree().getTechBudget()), budgetLocation, 0.33f);
		Font.drawString(gl, "Resources: " + (mGame.getCurrentPlayer().getResourcesRemaining()), resourcesRemainingLocation, 0.33f);

		for(int p = 0; p < GameConVars.TECH_TREES; ++p)
			for(int r = 0; r < GameConVars.TECH_DEPTH; ++r)
				for(int c = 0; c < 2; ++c)
					if(gridButtons[p][r][c] != null)
						if(techSlots[r * 2 + c]){
							if(mGame.getCurrentPlayer().getMyTechnologyTree().doesPlayerOwnTech(p, realTechNum[r*2+c])){
//								gridButtons[p][(r*2+c)/2][(r*2+c)%2].mImage = mHaveThisTech;
								// This is for when we get the images ready.
								mTechIcons[p][realTechNum[r*2+c]].draw(gl, gridButtons[p][r][c].mBackgroundDrawLocation);
//								gridButtons[p][r][c].draw(gl);
							}
							else{
//								gridButtons[p][(r*2+c)/2][(r*2+c)%2].mImage = mDoNotHaveThisTech;
//								gridButtons[p][r][c].getBG().draw(gl, gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y, 0.5f, 0.5f, 0.5f, 1f, 0);
//								gridButtons[p][r][c].getImage().draw(gl, gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y, 0.5f, 0.5f, 0.5f, 1f, 0);
								mTechIcons[p][realTechNum[r*2+c]].draw(gl, gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y, 0.4f, 0.4f, 0.4f, 1f, 0);

							}	
							Font.drawString(gl, gridButtons[p][r][c].getText(), new PointF(gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y - 0.3f), 0.25f);
							Font.drawString(gl, "Tech:" + realTechNum[r*2 + c], new PointF(gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y), 0.25f);
							Font.drawString(gl, "Tree:" + p, new PointF(gridButtons[p][r][c].mBackgroundDrawLocation.x, gridButtons[p][r][c].mBackgroundDrawLocation.y + 0.3f), 0.25f);
						}
		
//		shopButton.draw(gl);
	}
	
	public void drawTest(GL10 gl){
		if(nextTree != tree){
			tree = nextTree;
		}
		if(nextButtonNum != selectedButtonNum){
			selectedButtonNum = nextButtonNum;
		}
		
		mBackground.draw(gl, backgroundLocation);

		if(tree == -1){ // No Tree Selected
//			Font.drawString(gl, "Tree Selection", new PointF(5, 0.5f), 1);
			for(PressableButton treeIcon : treeButtons)
				if(treeIcon != null)
					treeIcon.draw(gl);
		}
		else if(selectedButtonNum == -1){ // No Tech Selected
//			Font.drawString(gl, "Tech Tree: " + tree, new PointF(5, 0.5f), 1);
			
			for(int r = 0; r < GameConVars.TECH_DEPTH; ++r)
				for(int c = 0; c < 2; ++c)
					if(gridButtons[tree][r][c] != null && realTechNum[r*2+c] != -1 && tree > -1)
						if(techSlots[r * 2 + c]){
							if(mGame.getCurrentPlayer().getMyTechnologyTree().doesPlayerOwnTech(tree, realTechNum[r*2+c])){
								Shapes.drawSquare(gl, gridButtons[tree][r][c].topRight, gridButtons[tree][r][c].botLeft, tree == 1 ? 1 : 0, tree == 0 ? 1 : 0, tree == 2 ? 1 : 0, .5f);
								mTechIcons[tree][realTechNum[r*2+c]].draw(gl, gridButtons[tree][r][c].mBackgroundDrawLocation);
							}
							else{
								Shapes.drawSquare(gl, gridButtons[tree][r][c].topRight, gridButtons[tree][r][c].botLeft, tree == 1 ? .75f : 0, tree == 0 ? .75f : 0, tree == 2 ? .75f : 0, .5f);
								mTechIcons[tree][realTechNum[r*2+c]].draw(gl, gridButtons[tree][r][c].mBackgroundDrawLocation.x, gridButtons[tree][r][c].mBackgroundDrawLocation.y, 0.4f, 0.4f, 0.4f, 1f, 0);
							}	
							Font.drawString(gl, gridButtons[tree][r][c].getText(), new PointF(gridButtons[tree][r][c].mBackgroundDrawLocation.x, gridButtons[tree][r][c].mBackgroundDrawLocation.y - 0.3f), 0.25f);
							Font.drawString(gl, "Tech:" + realTechNum[r*2 + c], new PointF(gridButtons[tree][r][c].mBackgroundDrawLocation.x, gridButtons[tree][r][c].mBackgroundDrawLocation.y), 0.25f);
							Font.drawString(gl, "Tree:" + tree, new PointF(gridButtons[tree][r][c].mBackgroundDrawLocation.x, gridButtons[tree][r][c].mBackgroundDrawLocation.y + 0.3f), 0.25f);
						}
			backButton.draw(gl);
		}
		else{ // Everything Selected
			selectedButton.getBG().draw(gl, mSelectedButtonLoc);

			// If owned
			if(mGame.getCurrentPlayer().getMyTechnologyTree().doesPlayerOwnTech(tree, (realTechNum[(selectedButtonNum - tree * realTechNum.length)])))
				Shapes.drawSquare(gl, mDiscBoxTopRight, mDiscBoxBotLeft, tree == 1 ? 1 : 0, tree == 0 ? 1 : 0, tree == 2 ? 1 : 0, .75f);
			// If not owned
			else
				Shapes.drawSquare(gl, mDiscBoxTopRight, mDiscBoxBotLeft, tree == 1 ? .75f : 0, tree == 0 ? .75f : 0, tree == 2 ? .75f : 0, .75f);
			
			mMenuBorder.draw(gl, mMenuBorderCenter);
			
			// Draw Description
			Font.drawStringBlockLeftAligned(gl, GameConVars.TECH_DETAILS[(tree * GameConVars.TECH_TREE_TOTALS + realTechNum[selectedButton.getmImageNum() % realTechNum.length]) * 2], new PointF(3.1f, 4.5f), 6, 0.4f);
//			Font.drawStringLeftAligned(gl, GameConVars.TECH_DETAILS[(tree * GameConVars.TECH_TREE_TOTALS + realTechNum[selectedButton.getmImageNum() % realTechNum.length]) * 2], new PointF(3.1f, 4.5f), .4f);
			Font.drawStringBlockLeftAligned(gl, GameConVars.TECH_DETAILS[(tree * GameConVars.TECH_TREE_TOTALS + realTechNum[selectedButton.getmImageNum() % realTechNum.length]) * 2 + 1], new PointF(3.1f, 4), 6, 0.4f);

			// Draw Back and Buy Buttons
			buyButton.draw(gl);
			backButton.draw(gl);
		}
	}
	
	private void setNextTree(int next){ nextTree = next; }
	private void setNextButton(int next){ nextButtonNum = next; }
	
	public void handleAction(int action, PointF glLocation){
		if(action == GameConVars.INPUT_TAP){
			PressableButton tappedButton = null;
			if(tree == -1){
				for(int t = 0; t < GameConVars.TECH_TREES; ++t)
					if(treeButtons[t].insideBox(glLocation))
						tappedButton = treeButtons[t];
			}
			else 
				for(int r = 0; r < GameConVars.TECH_DEPTH; ++r)
					for(int c = 0; c < 2; ++c)
						if(gridButtons[tree][r][c] != null && gridButtons[tree][r][c].insideBox(glLocation)) tappedButton = gridButtons[tree][r][c];

			if(tree > -1)
				if(backButton.insideBox(glLocation)) tappedButton = backButton;
			if(selectedButtonNum > -1)
				if(buyButton.insideBox(glLocation)) tappedButton = buyButton;
			
			if(tappedButton == null) return;
			
			int num = tappedButton.getmImageNum();
			
			if (num == -1){
				setNextTree(-1);
				setNextButton(-1);
				mGame.getGUI().toggleMenu(index);
			}
			else if(tree == -1){
				setNextTree(num);
			}
			else if(selectedButtonNum == -1){
				if(backButton.insideBox(glLocation)) setNextTree(-1);
				else if(techSlots[num % realTechNum.length]){
					selectedButton = gridButtons[tree][(num - tree * realTechNum.length) / 2][num%2];
					setNextButton(num);
				}
			}
			else{
				if(buyButton.insideBox(glLocation)){
					Logger.log(": " + realTechNum[(selectedButtonNum - tree * realTechNum.length)]);
					Logger.log("Tree: " + tree + " | " + "Tech: " + (realTechNum[(selectedButtonNum - tree * realTechNum.length)]));
					Logger.log("Get: " + mGame.getCurrentPlayer().purchaseTechnology(tree, realTechNum[(selectedButtonNum - tree * realTechNum.length)]));
				}
				if(backButton.insideBox(glLocation)) setNextButton(-1);
			}
		}
	}
	
	public void reset(){
		setNextTree(-1);
		setNextButton(-1);
//		tree = -1;
//		selectedButtonNum = -1;
	}

	public boolean insideBox(PointF p){
		return p.x >= mBotLeft.x && p.y >= mBotLeft.y & p.x <= mTopRight.x && p.y <= mTopRight.y;
	}
}
